﻿/// * Write a program that reads three integer numbers N, K and S and an array of N elements from the console. 
/// Find in the array a subset of K elements that have sum S or indicate about its absence.



using System;
using System.Collections.Generic;

class zad17
{
    static void Main()
    {
        int count = 0;
        int s = 14;
        int K = 4;
        int seqCount = 0;

        int[] arrInput = new int[] { 2, 1, 2, 4, 3, 5, 2, 6 };

        // new empty list
        List<int[]> subsetList = new List<int[]>();

        for (int i = 0; i < arrInput.Length; i++)
        {

            //count = 0;
            // get nuber of sequences
            int subsets = subsetList.Count;
            // add new list with current(i) element in it!
            int[] insert = new int[] { arrInput[i] };
            subsetList.Add(insert);

            for (int j = 0; j < subsets; j++)
            {
                // stop on first posible combination that has sum = s
                if (count < 1)
                {
                    // for every sub list of subsetList(e.g. all kombinations till now)
                    // copy this list in new list and add the current(i) element in it => arrInput[i]
                    // add newly created list in the subsetList colection.
                    int subsetLenght = subsetList[j].Length;
                    int[] newSubsetArrey = new int[subsetLenght + 1];
                    subsetList[j].CopyTo(newSubsetArrey, 0);
                    newSubsetArrey[newSubsetArrey.Length - 1] = arrInput[i];
                    subsetList.Add(newSubsetArrey);

                    // check if have allready reached the desired sum
                    foreach (var item in subsetList)
                    {
                        int sum = 0;
                        seqCount = 0;

                        for (int k = 0; k < item.Length; k++)
                        {
                            sum += item[k];
                            seqCount++;
                        }

                        if (sum == s && seqCount == K)
                        {
                            count++;

                            for (int a = 0; a < item.Length; a++)
                            {
                                Console.Write(item[a]);
                                if (a < item.Length - 1)
                                {
                                    Console.Write(", ");
                                }
                            }
                            Console.WriteLine();
                        }

                    }
                }
                else
                {
                    break;
                }
            }
        }

        if (count == 0)
        {
            Console.WriteLine("No such sum");
        }

    }
}

